#!/bin/sh

EXEC="./synap"

# Set time limit for all operations
#ulimit -t 30


Usage() {
    echo "Usage: runexamples.sh [options] [.syn files]"
    echo "-k    Keep intermediate files"
    echo "-h    Print this help"
    exit 1
}

SignalError() {
    if [ $error -eq 0 ] ; then
	echo "FAILED"
	error=1
    fi
    echo "  $1"
}

# Run <args>
# Report the command, run it, and report any errors
Run() {
    echo "Begin Running \"$*\""
    #echo $* 1>&2
    eval $* || {
	SignalError "$1 failed on $*"
	return 1
    }
    echo "Finished Running \"$1\""
}

Check() {
    error=0
    basename=`echo $1 | sed 's/.*\\///
                             s/.syn//'`
    reffile=`echo $1 | sed 's/.syn$//'`
    basedir="`echo $1 | sed 's/\/[^\/]*$//'`/."

    echo -n "$basename..."

    echo 1>&2
    echo "###### Testing $basename" 1>&2

    while read line
    do
	    ARGS=$line
	    break
    done < "../tests/${basename}.args"

    generatedfiles="${basename}.out" &&
    Run "$EXEC $ARGS" "<" $1

}

if [ $# -ge 1 ]
then
    files=$@
else
    files="../tests/ex-*.syn"
fi

for file in $files
do
    case $file in
	*/ex*)
	    echo $file
	    Check $file
	    ;;
	*)
	    echo $file
	    ;;
    esac
done

#echo "$numfail/$total tests failed"
exit $globalerror
